package com.jintoufs.web.action.scene;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.dao.QueryRule;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.domain.scene.SceneTicket;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.features.FeaturesService;
import com.jintoufs.service.scene.SceneTicketService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.web.action.shiro.bind.annotation.CurrentUser;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("ticket")
public class TicketAction extends SupportAction{

    @Resource
    private SceneTicketService sceneTicketService;
    @Resource
    private FeaturesService featuresService;

    @RequestMapping("list")
    public String list(String id,Model model) {
        model.addAttribute("sceneId",id);
        return "/scene/ticket/list";
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "鱼·定制产品")
    public void oper(HttpServletResponse response, String _id, String oper, SceneTicket sceneTicket,@CurrentUser Customer customer) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            switch (oper) {
                case "del":
                    sceneTicketService.delete(_id);
                    break;
                case "edit":
                    sceneTicketService.update(sceneTicket,customer);
                    ajaxMsg.setCode("200");
                    ajaxMsg.setMessage("更新成功");
                    break;
                case "add":
                    sceneTicket.setId(getUUID());
                    if (!ValidUtil.isEmpty(sceneTicket.getSceneId())){
                        sceneTicket.setStatus("1");
                        SceneTicket tmp = sceneTicketService.add(sceneTicket,customer);
                        if (tmp == null) {
                            ajaxMsg.setCode("500");
                            ajaxMsg.setMessage("添加鱼·定制产品失败");
                        } else {
                            ajaxMsg.setCode("200");
                            ajaxMsg.setMessage("添加鱼·定制产品成功");
                        }
                    }else {
                        ajaxMsg.setCode("500");
                        ajaxMsg.setMessage("添加鱼·定制产品失败");
                    }
                    break;
                default:
                    ajaxMsg.setCode("500");
                    ajaxMsg.setMessage("操作代码错误！系统只提供增加/修改/删除功能。");
                    break;

            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode("500");
            if (e instanceof DuplicateKeyException) {
//                ajaxMsg.setMessage("此酒店名已存在！");
//            } else {
                ajaxMsg.setMessage("程序出错，请联系管理员！");
            }
        }
        writeInfo(response, JSON.toJSONString(ajaxMsg));
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除鱼·定制产品")
    public void delete(HttpServletResponse response, String[] id) {
        for (String _id : id) {
            sceneTicketService.delete(_id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

    @RequestMapping("/query")
    @ResponseBody
    @SystemControllerLog(description = "获取鱼·定制产品列表")
    public JqgridPage query(String id, int page, int rows, String sidx,
                            String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        QueryRule role = new QueryRule();
        if (!ValidUtil.isEmpty(id)){
            role = new QueryRule();
            role.setOp("eq");
            role.setField("s.id");
            role.setData(id);
            filters_.getRules().add(role);
        }
        List<Map<String,Object>> list = sceneTicketService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }


    @RequestMapping("/input")
    @SystemControllerLog(description = "查看鱼·定制产品详细信息")
    public String input(String id, Model model,String isEdit,String sceneId) {
        Map<String, Object> result = new HashMap<>();
        Map<String,Object> param = new HashMap<>();
        if (!ValidUtil.isEmpty(id)) {
            param.put("valueId",id);
            result.put("sceneTicket", sceneTicketService.queryById(id));
            result.put("features", featuresService.selectByParams(param));
        }
        model.addAttribute("sceneId", sceneId);
        model.addAttribute("isEdit", isEdit);
        model.addAttribute("result", result);
        model.addAttribute("title", "鱼·定制产品详细信息");
        return "scene/ticket/input";
    }


}
